load("//bazel:mongo_src_rules.bzl", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files([
    "agg_join_model.h",
    "agg_join_model.cpp",
    "executor.h",
    "executor.cpp",
    "single_table_access.cpp",
    "single_table_access.h",
    "solution_storage.h",
])

exports_files(
    glob([
        "*.h",
    ]),
)

mongo_cc_library(
    name = "join_ordering",
    srcs = [
        "adjacency_matrix.cpp",
        "graph_cycle_breaker.cpp",
        "join_graph.cpp",
        "path_resolver.cpp",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/db/query:canonical_query_base",
        "//src/mongo/db/query/util:query_util_bitset",
        "//src/mongo/db/query/util:query_util_disjoint_set",
    ],
)

mongo_cc_library(
    name = "plan_enumerator",
    srcs = [
        "join_plan.cpp",
        "plan_enumerator.cpp",
        "plan_enumerator_helpers.cpp",
    ],
    hdrs = [
        "join_plan.h",
        "plan_enumerator.h",
        "plan_enumerator_helpers.h",
        "single_table_access.h",
    ],
    deps = [
        ":join_ordering",
        ":solution_storage",
        "//src/mongo/db/query/compiler/ce/sampling:sampling_estimator_interface",
        "//src/mongo/db/query/compiler/optimizer/cost_based_ranker:estimates",
    ],
)

mongo_cc_unit_test(
    name = "join_ordering_test",
    srcs = [
        "adjacency_matrix_test.cpp",
        "agg_join_model_test.cpp",
        "graph_cycle_breaker_test.cpp",
        "join_graph_test.cpp",
        "path_resolver_test.cpp",
        "plan_enumerator_test.cpp",
        "reorder_joins_test.cpp",
    ],
    data = [
        "//src/mongo/db/test_output/query/join/agg_join_model/pipeline_analyzer_test:test_data",
        "//src/mongo/db/test_output/query/join/join_plan_enumerator_test:test_data",
        "//src/mongo/db/test_output/query/join/reorder_graph_test:test_data",
    ],
    tags = ["mongo_unittest_third_group"],
    deps = [
        ":join_ordering",
        ":plan_enumerator",
        ":reorder_joins",
        ":unit_test_helpers",
        "//src/mongo/db:query_exec",
        "//src/mongo/db/pipeline:aggregation_context_fixture",
        "//src/mongo/db/shard_role/shard_catalog:index_catalog_mock",
    ],
)

mongo_cc_unit_test(
    name = "join_ordering_golden_test",
    srcs = [
        "agg_join_model_golden_test.cpp",
        "join_graph_golden_test.cpp",
    ],
    data = [
        "//src/mongo/db/test_output/query/compiler/optimizer/join/agg_join_model_golden_test:test_data",
        "//src/mongo/db/test_output/query/compiler/optimizer/join/join_graph_golden_test:test_data",
    ],
    tags = ["mongo_unittest_first_group"],
    deps = [
        ":join_ordering",
        ":unit_test_helpers",
        "//src/mongo/db:query_exec",
        "//src/mongo/db/pipeline:aggregation_context_fixture",
        "//src/mongo/db/query:query_request",
    ],
)

mongo_cc_library(
    name = "join_predicate",
    srcs = [
        "join_predicate.cpp",
    ],
    deps = [
        "//src/mongo/db/pipeline:field_path",
    ],
)

mongo_cc_unit_test(
    name = "single_table_access_test",
    srcs = [
        "single_table_access_test.cpp",
    ],
    tags = ["mongo_unittest_fifth_group"],
    deps = [
        ":solution_storage",
        ":unit_test_helpers",
        "//src/mongo/db:query_exec",
        "//src/mongo/db/query/compiler/ce/sampling:sampling_test_utils",
    ],
)

mongo_cc_library(
    name = "reorder_joins",
    srcs = [
        "reorder_joins.cpp",
    ],
    deps = [
        ":plan_enumerator",
        ":solution_storage",
        "//src/mongo/db/query:random_utils",
    ],
)

mongo_cc_library(
    name = "solution_storage",
    deps = [
        "//src/mongo/db/query:canonical_query_base",
    ],
)

mongo_cc_library(
    name = "unit_test_helpers",
    srcs = [
        "unit_test_helpers.cpp",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/db/pipeline",
        "//src/mongo/db/shard_role/shard_catalog:catalog_test_fixture",
        "//src/mongo/unittest",
    ],
)
